//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "StochasticProcess.h"
using namespace Cephei::QL;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CStochasticProcess::CStochasticProcess (boost::shared_ptr<QuantLib::StochasticProcess>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phStochasticProcess = NULL;
#endif
	_ppStochasticProcess = &childNative;
    
}
Cephei::QL::CStochasticProcess::CStochasticProcess (QuantLib::StochasticProcess& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phStochasticProcess = NULL;
#endif
	_ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (&childNative);
    
    _StochasticProcessOwner = owner;
    
}

Cephei::QL::CStochasticProcess::CStochasticProcess (CStochasticProcess^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phStochasticProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (copy->GetShared());
        
    }
}
Cephei::QL::CStochasticProcess::CStochasticProcess (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phStochasticProcess = NULL;
#endif
	if (!t->IsSubclassOf(CStochasticProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CStochasticProcess::CStochasticProcess (QuantLib::Handle<QuantLib::StochasticProcess>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phStochasticProcess = &childNative;
	_ppStochasticProcess = &static_cast<boost::shared_ptr<QuantLib::StochasticProcess>>(childNative.currentLink());
    
    _StochasticProcessOwner = owner;
}
Cephei::QL::CStochasticProcess::CStochasticProcess (QuantLib::Handle<QuantLib::StochasticProcess> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phStochasticProcess = &childNative;
	_ppStochasticProcess = &static_cast<boost::shared_ptr<QuantLib::StochasticProcess>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CStochasticProcess::CStochasticProcess (QuantLib::StochasticProcess childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phStochasticProcess = NULL;
#endif
	_ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (new QuantLib::StochasticProcess (childNative));
    
}
#endif

Cephei::QL::CStochasticProcess::~CStochasticProcess ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppStochasticProcess != NULL)
    {
	    delete _ppStochasticProcess;
        _ppStochasticProcess = NULL;
    }
}
Cephei::QL::CStochasticProcess::!CStochasticProcess ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppStochasticProcess != NULL)
    {
	    delete _ppStochasticProcess;
    }
}
QuantLib::StochasticProcess& Cephei::QL::CStochasticProcess::GetReference ()
{
    if (_ppStochasticProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppStochasticProcess;
}
boost::shared_ptr<QuantLib::StochasticProcess>& Cephei::QL::CStochasticProcess::GetShared ()
{
    if (_ppStochasticProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppStochasticProcess;
}
QuantLib::StochasticProcess* Cephei::QL::CStochasticProcess::GetPointer ()
{
    if (_ppStochasticProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppStochasticProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::StochasticProcess>& Cephei::QL::CStochasticProcess::GetHandle ()
{
	if (_phStochasticProcess == NULL)
	{
		_phStochasticProcess = new Handle<QuantLib::StochasticProcess> (*_ppStochasticProcess);
	}
	return *_phStochasticProcess;
}
#endif
bool Cephei::QL::CStochasticProcess::HasNative () 
{
	return (_ppStochasticProcess != NULL);
}

UInt64 Cephei::QL::CStochasticProcess::Factors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppStochasticProcess)->factors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::CStochasticProcess::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppStochasticProcess)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CStochasticProcess::Time (DateTime prm1)
{
    try
    {
        QuantLib::Date _prm1 = (QuantLib::Date)ValueHelper::Convert (prm1); //a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppStochasticProcess)->time ( _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IStochasticProcess^ Cephei::QL::CStochasticProcess::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppStochasticProcess)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CStochasticProcessVector::CStochasticProcessVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::CStochasticProcessVector::~CStochasticProcessVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::CStochasticProcessVector::!CStochasticProcessVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::CStochasticProcessVector::CStochasticProcessVector (std::vector<QuantLib::StochasticProcess>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::CStochasticProcessVector::CStochasticProcessVector (std::vector<QuantLib::StochasticProcess> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::StochasticProcess> (vec);
}
#endif
Cephei::QL::CStochasticProcessVector::CStochasticProcessVector (std::vector<boost::shared_ptr<QuantLib::StochasticProcess>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::CStochasticProcessVector::CStochasticProcessVector (std::vector<QuantLib::Handle<QuantLib::StochasticProcess>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::CStochasticProcessVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::IStochasticProcess^ Cephei::QL::CStochasticProcessVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::StochasticProcess& native = (*_pVValue)[index];
		return REFNEW CStochasticProcess (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::StochasticProcess& native = *(*_pVBoost)[index];
		return REFNEW CStochasticProcess (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::StochasticProcess& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CStochasticProcess (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::CStochasticProcessVector::default::set (int index, Cephei::QL::IStochasticProcess^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CStochasticProcess^>(value)->GetReference ());
		}
        else
        {
            QuantLib::StochasticProcess& c = (*_pVValue)[index];
            c = dynamic_cast<CStochasticProcess^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CStochasticProcess^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::StochasticProcess>& c = (*_pVBoost)[index];
		c = dynamic_cast<CStochasticProcess^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::StochasticProcess> ());
		}
		QuantLib::Handle<QuantLib::StochasticProcess>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::StochasticProcess>& b = static_cast<boost::shared_ptr<QuantLib::StochasticProcess>>(c.currentLink());
		b = dynamic_cast<CStochasticProcess^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^ Cephei::QL::CStochasticProcessVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<IStochasticProcess^> (dynamic_cast<IStochasticProcess^>(default[index]));
}

void Cephei::QL::CStochasticProcessVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^ value) 
{
	default[index] = safe_cast<IStochasticProcess^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::StochasticProcess>& Cephei::QL::CStochasticProcessVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::StochasticProcess>>& Cephei::QL::CStochasticProcessVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::StochasticProcess>>& Cephei::QL::CStochasticProcessVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::CStochasticProcessVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::StochasticProcess> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::StochasticProcess>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::StochasticProcess>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

/* <summary>Create a single dimension array of StochasticProcess that can be passed into other function </summary> */
IVector<Cephei::QL::IStochasticProcess^>^ Cephei::QL::CStochasticProcess_Factory::CreateVector ()
 {
	 return REFNEW CoVector<IStochasticProcess^> (REFNEW CStochasticProcessVector ());
 }
IVector<Cephei::QL::IStochasticProcess^>^ Cephei::QL::CStochasticProcess_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<IStochasticProcess^>^ source)
 {
	 return REFNEW CoVector<IStochasticProcess^> (source, REFNEW CStochasticProcessVector ());
 }
/* <summary>Create a single dimension array of StochasticProcess with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^>^>^ Cephei::QL::CStochasticProcess_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^>^> (REFNEW CoCellVector<Cephei::QL::IStochasticProcess^> (REFNEW CStochasticProcessVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^>^>^ Cephei::QL::CStochasticProcess_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^>^> (REFNEW CoCellVector<Cephei::QL::IStochasticProcess^> (source, REFNEW CStochasticProcessVector ()));
 }

